﻿using log4net.Appender;

namespace NServiceBus.Hosting.Windows.LoggingHandlers
{
    using System;
    using System.Runtime.InteropServices;
    using log4net.Core;

    /// <summary>
    /// Handles logging configuration for the production profile
    /// </summary>
    public class ProductionLoggingHandler : IConfigureLoggingForProfile<Production>
    {
        void IConfigureLogging.Configure(IConfigureThisEndpoint specifier)
        {
            SetLoggingLibrary.Log4Net<RollingFileAppender>(null,
                a =>
                {
                    a.CountDirection = 1;
                    a.DatePattern = "yyyy-MM-dd";
                    a.RollingStyle = RollingFileAppender.RollingMode.Composite;
                    a.MaxFileSize = 1024 * 1024;
                    a.MaxSizeRollBackups = 10;
                    a.LockingModel = new FileAppender.MinimalLock();
                    a.StaticLogFileName = true;
                    a.File = "logfile";
                    a.AppendToFile = true;
                });

            if (GetStdHandle(STD_OUTPUT_HANDLE) == IntPtr.Zero)
                return;

            SetLoggingLibrary.Log4Net<ColoredConsoleAppender>(null,
              a =>
              {
                  LiteLoggingHandler.PrepareColors(a);
                  a.Threshold = Level.Info;
              }
          );
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr GetStdHandle(int nStdHandle);
        const int STD_OUTPUT_HANDLE = -11;
    }
}